/*
* name;
*/
class WorldMap{
    private _gridVO:MapGridVO;
    private _mapUrl:string = "res/map/map_001.jpg";
    private static _instance:WorldMap;
    private _container:Sprite;
    private _gridArray:Array<Array<MapTile>>
    private constructor(){
        this.init();
    }

    public static get instance():WorldMap
    {
        if(!this._instance)
        {
            this._instance = new WorldMap();
        }
        return this._instance;
    }

    
    public get container() : Sprite {
        return this._container
    }
    
    /**
     * 地图数据
     */
    public get gridVO():MapGridVO
    {
        return this._gridVO;
    }

    /**
     *  地图块集合 
     */
    public gridArray():Array<Array<MapTile>>
    {
        return this._gridArray;
    }

    public init():void
    {
        this._container =  new Sprite();
        this._gridVO = new MapGridVO();
        this.creatTitle();
    }

    public updata():void
    {
        let cameraView:Laya.Rectangle =  SceneManager.instance.camera2D.cameraView;
        let titles = this.getNeedLoadTitle(cameraView.x,cameraView.y);
        // console.log(cameraView.x,"----",cameraView.y)
        this.loadTitle(titles);
    }

    /**
     * 创建地图块
     */
    public creatTitle():void
    {
        this._gridArray = new Array<Array<MapTile>>();
        let rowLen:number = this._gridVO.row;
        let colLen:number = this._gridVO.col
        for(let i:number = 0;i < colLen; i++){
            let array = new Array<MapTile>();
            for(let j:number = 0;j < rowLen; j++){
                let maoTitle:MapTile = new MapTile(i,j,this._container);
                array.push(maoTitle);
            }
            this._gridArray.push(array);
        }
    }

    /**
     * 获取需要加载的地图块
     * @param camX 相机X
     * @param camY 相机Y
     */
    public getNeedLoadTitle(camX:number,camY:number):Dictionary
    {
        let cellW:number = this._gridVO.cellW;
        let cellH:number = this._gridVO.cellH;
        let rect:Laya.Rectangle =  new Laya.Rectangle(camX - cellW,camY-cellH,GameConfig.deviceW + cellW,GameConfig.deviceH + cellH)
        let p1:Laya.Point = this.scenePosToGrid(rect.x,rect.y);
        let p2:Laya.Point = this.scenePosToGrid(rect.right,rect.bottom);
        let res:Dictionary = new Dictionary();
        let key:string;
        for(let i:number = p1.x;i <= p2.x; i++){
            for(let j:number = p1.y;j <=  p2.y; j++){
                key = i+"_"+j; //X切割方向_Y切割方向
                res.set(key,key)
            }
        }
        return res;
    }

    /**
     * 
     * @param x 场景坐标的X
     * @param y 场景坐标的y
     */
    public scenePosToGrid(x:number,y:number):Laya.Point
    {
        let point:Laya.Point = new Laya.Point();
        point.x = parseInt(x/this._gridVO.cellW + "");
        point.y = parseInt(y/this._gridVO.cellH + "");
        return point;
    }

    private loadedTiles:Dictionary = new Dictionary();
    /**
     * 
     */
    public loadTitle(tiles:Dictionary):void
    {   
        if(tiles && tiles.keys.length > 0)
        {//先卸载
            let loadedlen:number = this.loadedTiles.keys.length;
            for(let i:number = loadedlen - 1;i >= 0; i--)
            {//遍历已加载地图块
                let loadedKey:string = this.loadedTiles.keys[i];
                if(tiles.keys.indexOf(loadedKey) == -1){//即将加载地图块中没有,则卸载
                    let arr = loadedKey.split("_");
                    let x = parseInt(arr[0]);
                    let y = parseInt(arr[1]);
                    console.log("卸载:",loadedKey);
                    (this._gridArray[y][x]  as MapTile).dispose();
                    this.loadedTiles.remove(loadedKey);
                }
            }
            //加载

            let len:number = tiles.keys.length;
            for(let j:number = 0;j < len; j++){
                let key:string = tiles.keys[j];
                if(this.loadedTiles.keys.indexOf(key) >= 0)
                {//已加载过地图块则不加载
                    continue;
                }
                let arr = key.split("_");
                let x = parseInt(arr[0]);
                let y = parseInt(arr[1])
                if(y < this._gridVO.col && x < this._gridVO.row)
                {
                    (this._gridArray[y][x] as MapTile).loadTile();
                     console.log("加载:",key);
                    this.loadedTiles.set(key,key)
                }
            }
        }
    }
}